//
//  HGFansVC.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/8/6.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGFansVC.h"
#import "HGFansCell.h"
#import "HGFansModel.h"

@interface HGFansHeadeView : UIView

- (void)TodayCount:(NSInteger)todayCount amountCount:(NSInteger)amountCount;

@end

@implementation HGFansHeadeView {
    UILabel *_newFansNumLabel;//新增
    UILabel *_fansNumCountLabel;//累计
}

- (instancetype)init {
    if (self = [super init]) {
        [self initSubViews];
    }
    return self;
}

- (void)TodayCount:(NSInteger)todayCount amountCount:(NSInteger)amountCount {
    _newFansNumLabel.text = [NSString stringWithFormat:@"%ld\n今日新增粉丝数",todayCount];
    _fansNumCountLabel.text = [NSString stringWithFormat:@"%ld\n累计粉丝数",amountCount];
    
    [Utility fuwenbenLabel:_newFansNumLabel fontNumber:[UIFont systemFontOfSize:30] andRange:NSMakeRange(0, @(todayCount).description.length) andColor:kColorMainTheme];
    
    [Utility fuwenbenLabel:_fansNumCountLabel fontNumber:[UIFont systemFontOfSize:30] andRange:NSMakeRange(0, @(amountCount).description.length) andColor:kColorMainTheme];
}

- (void)initSubViews {
    
    _newFansNumLabel = [[UILabel alloc] init];
    _newFansNumLabel.text = @"0\n今日新增粉丝数";
    _newFansNumLabel.textColor = kColorMainTheme;
    _newFansNumLabel.textAlignment = NSTextAlignmentCenter;
    _newFansNumLabel.numberOfLines = 2;
    _newFansNumLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:_newFansNumLabel];
    
    [_newFansNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).multipliedBy(0.5);
        make.centerY.equalTo(self).multipliedBy(0.9);
    }];
    
    _fansNumCountLabel = [[UILabel alloc] init];
    _fansNumCountLabel.text = @"0\n累计粉丝数";
    _fansNumCountLabel.textColor = kColorMainTheme;
    _fansNumCountLabel.textAlignment = NSTextAlignmentCenter;
    _fansNumCountLabel.numberOfLines = 2;
    _fansNumCountLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:_fansNumCountLabel];
    
    [_fansNumCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).multipliedBy(1.5);
        make.centerY.equalTo(self).multipliedBy(0.9);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = kColorBackground;
    [self addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.offset(10);
    }];
}

@end


@interface HGFansVC ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) HGFansHeadeView *headerView;

@property (nonatomic,strong)UITableView* tableView;

@property (nonatomic, copy) NSMutableArray <HGFansModel *> *fansModels;

@end

@implementation HGFansVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initDatas];
    
    [self.tableView registerClass:[HGFansCell class] forCellReuseIdentifier:@"HGFansCell"];
}

- (void)initDatas {
    [[LMJRequestManager sharedManager] GET:KBaseURL(@"/app/merge/list") parameters:nil completion:^(LMJBaseResponse *response) {
        
        if (response.responseModel.status == 1) {
            _fansModels = [HGFansModel mj_objectArrayWithKeyValuesArray:response.responseModel.data[@"listFans"]];
            [_headerView TodayCount:[response.responseModel.data[@"todayFans"] integerValue] amountCount:[response.responseModel.data[@"amountFans"] integerValue]];
            [_tableView reloadData];
        }
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HGFansCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HGFansCell"];
    cell.model = _fansModels[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _fansModels.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HGFansVC *vc = [[HGFansVC alloc] init];
    vc.title = @"我的二级粉";
    vc.type = 1;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UITableViewHeaderFooterView *hederView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"UITableViewHeaderFooterView"];
    hederView.contentView.backgroundColor = [UIColor whiteColor];
    UIButton *hederButton = [[UIButton alloc] init];
    [hederButton setTitle:@" 我的一级粉" forState:(UIControlStateNormal)];
    [hederButton setTitleColor:kColorDefult forState:(UIControlStateNormal)];
    hederButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [hederButton setImage:[UIImage imageNamed:@"tip"] forState:(UIControlStateNormal)];
    [hederView.contentView addSubview:hederButton];
    
    [hederButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(hederView);
        make.left.offset(15);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = kColorBackground;
    [hederView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(hederView);
        make.height.offset(1);
        make.left.offset(15);
    }];
    
    return hederView;
}

- (UITableView *)tableView {
    if (!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 60;
        _tableView.tableFooterView = [UIView new];
        _tableView.tableHeaderView = self.headerView;
        [_tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"UITableViewHeaderFooterView"];
        _tableView.sectionHeaderHeight = 40;
        [self.view addSubview:_tableView];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(_type == 0 ? 44 : 0);
            make.bottom.left.right.equalTo(self.view);
        }];
    }
    return _tableView;
}

- (HGFansHeadeView *)headerView {
    if (!_headerView) {
        _headerView = [[HGFansHeadeView alloc] init];
        _headerView.height = 100;
    }
    return _headerView;
}

- (NSMutableArray<HGFansModel *> *)fansModels {
    if (!_fansModels) {
        _fansModels = [@[] mutableCopy];
    }
    return _fansModels;
}

@end
